package baidu

import (
	"encoding/json"
	"io/ioutil"
	"request"
)

/**
 * json结构体
 */
type wordsGeneral struct {
	Words_general struct {
		Url     string
		Headers []struct {
			Key   string
			Value string
		}
	}
}

/**
 * 图片转文字类
 */
func BasicGeneral(data map[string]interface{}) string {

	// 返回值
	words := ""

	// 请求百度接口
	url, headers := packageWordsGeneralData()
	data["access_token"] = GetAccessToken()
	res := request.Post(url, headers, data, "general")

	// 拼接数据
	if res["words_result_num"].(float64) == 0 {
		return words
	}

	// 拼装返回数据
	for _, value := range res["words_result"].([]interface{}) {
		words += value.(map[string]interface{})["words"].(string)
	}

	return words
}

/**
 * 解析配置文件数据
 */
func packageWordsGeneralData() (string, map[string]string) {

	message, _ := ioutil.ReadFile("config/baiduai.json")
	wordsGeneral := &wordsGeneral{}
	json.Unmarshal([]byte(message), &wordsGeneral)

	// 数据转换
	headers := make(map[string]string)
	for _, header := range wordsGeneral.Words_general.Headers {
		headers[header.Key] = header.Value
	}

	return wordsGeneral.Words_general.Url, headers
}
